class RobotStrategy {
    static hu(gt, player) {
        gt.player_op_hu(player);
    }

    static ting(gt, player, card) {
        gt.player_op_ting(player);
        gt.playerChuOperation(player, card);
    }

    static cancel(gt, player) {
        gt.player_cancel_chi_peng_gang_hu(player);
    }

    static gang(gt, player, card) {
        gt.player_op_gang(player, card);
    }

    static peng(gt, player) {
        gt.player_op_peng(player);
    }

    static chu(gt, player) {
        let cards = [...player.cardsInHand];
        if (player.getCardGrab() !== 0) {
            cards.push(player.getCardGrab());
        }
        cards.sort((card1, card2) => card1 - card2);
        gt.playerChuOperation(player, this.findTargetCard(cards));
    }

    // static findTargetCard(cards) {
    //     let arrays = [];
    //     for (let i = 0, j = 0; i < cards.length - 1 ; i++) {
    //         let array = [cards[i]];
    //         for (j = i ; j < cards.length - 1 ; j++, i++) {
    //             if (cards[j] === cards[j+1] || cards[j] + 1 === cards[j+1]) {
    //                 array.push(cards[j]);
    //             } else {
    //                 break;
    //             }
    //         }
    //         arrays.push(array);
    //         // let array = [cards[i]];
    //         // if (cards[i] === cards[i+1] || cards[i] + 1 === cards[i+1]) {
    //         //     array.push(cards[i+1]);
    //         // } else {
    //         //     array
    //         // }
    //         // arrays.push(array);
    //     }
    //
    //     // let arraysLength = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]];
    //
    //     let arrays1 = [];
    //     let arrays4 = [];
    //
    //     for (let array of arrays) {
    //         if (array.length === 1) {
    //             arrays1.push(array);
    //         }
    //         if (array.length === 4) {
    //             arrays4.push(array);
    //         }
    //     }
    //
    //     if (arrays1.length !== 0) {
    //         for (let array of arrays1) {
    //             if ((array[0] | 0x0f === 0x01) || (array[0] | 0x0f === 0x09)) {
    //                 return array[0];
    //             }
    //         }
    //         return arrays1[0][0];
    //     }
    //     if (arrays4.length !== 0) {
    //         let array = arrays4[0];
    //         if (array[0] + 1 === array[1] && array[1] + 1 === array[2] && array[2] + 1 === array[3]) {
    //             return array[0];
    //         }
    //         if (array[0] === array[1] && array[1] + 1 === array[2] && array[2] + 1 === array[3]) {
    //             return array[0];
    //         }
    //         if (array[0] + 1 === array[1] && array[1] === array[2] && array[2] + 1 === array[3]) {
    //             return array[1];
    //         }
    //         if (array[0] + 1 === array[1] && array[1] + 1 === array[2] && array[2] === array[3]) {
    //             return array[2];
    //         }
    //     }
    //     return cards[0];
    // }

    static findTargetCard(cards) {
        let card = this.findSingleCard(cards);
        if (card !== 0) {
            return card;
        }
        card = this.findLinkCard(cards);
        if (card !== 0) {
            return card;
        }
        return cards[0];
    }

    static findSingleCard(cards) {
        let singleCards = [];
        if (cards[0] !== cards[1] && cards[0] + 1 !== cards[1]) {
            // return cards[0];
            singleCards.push(cards[0]);
        }
        for (let i = 1; i < cards.length - 1; i++) {
            if (cards[i] !== cards[i + 1] && cards[i] + 1 !== cards[i + 1] && cards[i - 1] !== cards[i] && cards[i - 1] + 1 !== cards[i]) {
                // return cards[i];
                singleCards.push(cards[i]);
            }
        }
        if (cards[cards.length - 2] !== cards[cards.length - 1] && cards[cards.length - 2] + 1 !== cards[cards.length - 1]) {
            // return cards[cards.length - 1];
            singleCards.push(cards[cards.length - 1]);
        }
        if (singleCards.length === 0) {
            return 0;
        }
        //优先出1 9万条筒
        for (let singleCard of singleCards) {
            if (singleCard | 0x0f === 0x01 || singleCard | 0x0f === 0x09) {
                return singleCard;
            }
        }
        return singleCards[0];

    }

    static findLinkCard(cards) {
        for (let i = 0; i < cards.length - 1; i++) {
            // if (cards[i] + 1 === cards[i + 1] && ) {
            //     return cards[i];
            // }
        }
        return 0;
    }
}

module.exports = RobotStrategy;